package edu.cs.usask.ca.silver.plugin.rcp.pages.preferences;

import org.eclipse.jface.preference.FieldEditorPreferencePage;
import org.eclipse.jface.preference.StringFieldEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import edu.cs.usask.ca.silver.plugin.rcp.Activator;
import edu.cs.usask.ca.silver.plugin.rcp.components.SilverPreferenceEntries;

public class UserPreferencePage extends FieldEditorPreferencePage implements
		IWorkbenchPreferencePage {

	public UserPreferencePage() {
		super(GRID);
	}

	@Override
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	@Override
	protected void createFieldEditors() {
		addField(new StringFieldEditor(
				SilverPreferenceEntries.USER_EMAIL_LOGIN.name(),
				SilverPreferenceEntries.USER_EMAIL_LOGIN
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.USER_EMAIL_LOGIN
						.getLabelWidthToBeArranged(), getFieldEditorParent()));

		StringFieldEditor passwdField = new StringFieldEditor(
				SilverPreferenceEntries.USER_PASSWORD.name(),
				SilverPreferenceEntries.USER_PASSWORD
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.USER_PASSWORD
						.getLabelWidthToBeArranged(), getFieldEditorParent());

		passwdField
				.setValidateStrategy(StringFieldEditor.VALIDATE_ON_FOCUS_LOST);

		passwdField.getTextControl(getFieldEditorParent()).setEchoChar('*');

		addField(passwdField);

		addField(new StringFieldEditor(
				SilverPreferenceEntries.USER_FIRST_NAME.name(),
				SilverPreferenceEntries.USER_FIRST_NAME
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.USER_FIRST_NAME
						.getLabelWidthToBeArranged(), getFieldEditorParent()));

		addField(new StringFieldEditor(
				SilverPreferenceEntries.USER_LAST_NAME.name(),
				SilverPreferenceEntries.USER_LAST_NAME
						.getLabelTextToBeDisplayed(),
				SilverPreferenceEntries.USER_LAST_NAME
						.getLabelWidthToBeArranged(), getFieldEditorParent()));

		Button updateButton = new Button(this.getFieldEditorParent(), SWT.PUSH);
		GridData data = new GridData(SWT.RIGHT, SWT.BOTTOM, true, true);
		data.horizontalSpan = 2;

		updateButton.setText("Update user information in database");
		updateButton.setLayoutData(data);
		updateButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				checkUpdate();
			}
		});
	}

	protected void checkUpdate() {

	}

}
